package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.*;
import com.ytjj.qmyx.admin.model.response.ProductActivityModelResponse;
import com.ytjj.qmyx.admin.service.ProductActivityService;
import com.ytjj.qmyx.mall.model.ProductActivity;
import com.ytjj.qmyx.mall.model.ProductActivityImg;
import com.ytjj.qmyx.mall.model.ProductActivityMatching;
import com.ytjj.qmyx.mall.model.ProductActivitySpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品活动管理
 */
@RestController
@RequestMapping("/productActivity")
public class ProductActivityController {

    @Autowired
    private ProductActivityService productActivityService;

    /**
     * 查询活动列表
     * @param activityRequest
     * @return
     */
    @PostMapping(value = "/activityList")
    public CommonResult activityList(@RequestBody ActivityRequest activityRequest) {
        CommonPage<ProductActivityModelResponse> pageList = productActivityService.activityList(activityRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑活动
     * @param productActivity
     * @return
     */
    @PostMapping(value = "/updateActivity")
    @OperLog(operModule = "活动模块", operDesc = "编辑活动", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateActivity(@RequestBody ProductActivity productActivity) {
        int count = productActivityService.updateActivity(productActivity);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 活动 上架/下架
     * @param id
     * @return
     */
    @GetMapping(value = "/updateActivityStatus")
    @OperLog(operModule = "活动模块", operDesc = "活动上架/下架", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateActivityStatus(@RequestParam Integer id) {
        int count = productActivityService.updateActivityStatus(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 移除商品活动
     * @param id
     * @return
     */
    @GetMapping("/delProductActivity")
    public CommonResult delProductActivity(@RequestParam Integer id) {
        return CommonResult.success(productActivityService.delProductActivity(id));
    }

    /**
     * 根据商品活动id 查询商品活动规格列表
     * @param activitySpectRequest
     * @return
     */
    @PostMapping(value = "/getActivitySpecsById")
    public CommonResult getActivitySpecsById(@RequestBody ActivitySpectRequest activitySpectRequest) {
        CommonPage<ProductActivitySpec> pageList = productActivityService.getActivitySpecsById(activitySpectRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑商品活动规格
     * @param productActivitySpec
     * @return
     */
    @PostMapping(value = "/updateActivitySpec")
    @OperLog(operModule = "活动模块", operDesc = "编辑商品活动规格", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateActivitySpec(@RequestBody ProductActivitySpec productActivitySpec) {
        int count = productActivityService.updateActivitySpec(productActivitySpec);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    // -------------------------------------------------------商品活动图片列表---------------------------------------------------------------------

    /**
     * 根据商品活动id 查询商品活动图片列表
     * @param activityImgRequest
     * @return
     */
    @PostMapping(value = "/getActivityImgsById")
    public CommonResult getActivityImgsById(@RequestBody ActivityImgRequest activityImgRequest) {
        CommonPage<ProductActivityImg> pageList = productActivityService.getActivityImgsById(activityImgRequest);
        return CommonResult.success(pageList);
    }


    /**
     * 新增商品活动图片
     * @param productActivityImgRequest
     * @return
     */
    @PostMapping(value = "/addActivityImg")
    @OperLog(operModule = "活动模块", operDesc = "新增商品活动图片", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addActivityImg(@RequestBody ProductActivityImgRequest productActivityImgRequest) {
        int count = productActivityService.addActivityImg(productActivityImgRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 编辑商品活动图片
     * @param productActivityImg
     * @return
     */
    @PostMapping(value = "/updateActivityImg")
    @OperLog(operModule = "活动模块", operDesc = "编辑商品活动图片", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateActivityImg(@RequestBody ProductActivityImg productActivityImg) {
        int count = productActivityService.updateActivityImg(productActivityImg);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping(value = "/deleteActivityImg")
    @OperLog(operModule = "活动模块", operDesc = "删除商品活动图片", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteActivityImg(@RequestParam("id") Integer id){
        productActivityService.deleteActivityImg(id);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    // ------------------------------------------------------------------活动推荐信息-------------------------------------------------------------------------------------

    /**
     * 查询活动商品推荐信息
     * @param productId
     * @return
     */
    @GetMapping(value = "/getProductActivityMatching")
    public CommonResult getProductActivityMatching(@RequestParam Integer productId) {
        ProductActivityMatching productActivityMatching = productActivityService.getProductActivityMatching(productId);
        return CommonResult.success(productActivityMatching);
    }


    /**
     * 修改活动商品推荐信息
     * @param productActivityMatching
     * @return
     */
    @PostMapping(value = "/updateProductActivityMatching")
    @OperLog(operModule = "店家商品模块", operDesc = "修改活动商品推荐信息", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult updateProductActivityMatching(@RequestBody ProductActivityMatching productActivityMatching) {
        int count = productActivityService.updateProductActivityMatching(productActivityMatching);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    /**
     * 批量set stock
     * @param stock
     * @return
     */
    @GetMapping(value = "/updateStockBatch")
    @OperLog(operModule = "店家商品模块", operDesc = "批量设置活动商品库存", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult updateStockBatch(@RequestParam Integer stock) {
        int count = productActivityService.updateStockBatch(stock);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 批量修改商品规格中的价格和库存
     * 同时商品的总库存需同步
     * @param batchRequest
     * @return
     */
    @PostMapping(value = "/updatePriceStock")
    public CommonResult updatePriceStock(@RequestBody ProductBatchRequest batchRequest){
        int count = productActivityService.updatePriceStock(batchRequest.getProductId(), batchRequest.getPrice(), batchRequest.getStock());
        //批量修改库存后--同步商品总库存数
        int allCount = count * batchRequest.getStock();
        if (count > 0) {
            productActivityService.updateStock(batchRequest.getProductId(), allCount);
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    // ---------------------------------------  同步商品规格--直接购买价----------------------------------
    @PostMapping("/syncSpecInfo")
    public CommonResult syncSpecInfo() {
        return CommonResult.success(productActivityService.syncSpecInfo());
    }

}
